from ..async_tcp_server import serverInQueue, serverOutQueue
import asyncio
import re


class BlobControl:

    def __init__(self):
        self.__roomName: str = '客厅'
        self.__colorCode: str = '#ffffff'

    def getRoomName(self):
        return self.__roomName

    def getColorCode(self):
        return self.__colorCode

    def setRoomName(self, room: str):
        self.__roomName = room

    async def setColorCode(self, color: str):
        self.__colorCode = color
        cmd = self.__changeCMD()
        await self.__sendCMD(cmd)

    def __changeCMD(self):
        color_str = self.__colorCode[1:7]
        color_list = re.findall(r'.{2}', color_str)
        color_string = ''
        color_string += ('R' + str(int(color_list[0], 16)))
        color_string += ('G' + str(int(color_list[1], 16)))
        color_string += ('B' + str(int(color_list[2], 16)))
        print(f"发送的颜色字符串为->{color_string}")
        return f"{color_string}\n"

    async def __sendCMD(self, cmd):
        serverInQueue.put(cmd)
        res_status = "F"
        while res_status != 'T':
            if not serverOutQueue.empty():
                res_status = serverOutQueue.get()
            else:
                await asyncio.sleep(0)
